package com.bjmashibing.system.io;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;

/**
 * @author: 马士兵教育
 * @create: 2020-05-17 16:18
 */
public class SocketClient {

    public static void main(String[] args) {

        try {

//            SocketChannel client = SocketChannel.open();
//            client.bind(new InetSocketAddress("192.168.72.1", 10087));
//            client.connect(new InetSocketAddress("192.168.72.66",9090));
            Socket client = new Socket("localhost", 9090);
//            client.bind(new InetSocketAddress("172.18.214.36", 10087));
//            client.connect(new InetSocketAddress("192.168.72.66",9090));
            client.setSendBufferSize(20);
            client.setTcpNoDelay(false);
            client.setOOBInline(false);//true时紧急数据与普通数据一般一起接受
            OutputStream out = client.getOutputStream();

            InputStream in = System.in;
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            while(true){
                String line = reader.readLine();
                if(line.equals("66")){
                    client.close();
                    break;
                }
                System.out.println("======");
                if(line != null ){
                    byte[] bb = line.getBytes();
                    System.out.println(bb.length);
                    for (byte b : bb) {
                        out.write(b);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
